package com.sgcc.sx.pmos.e_sx_mall.adapter;


import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sgcc.sx.pmos.common.bean.ComparisonDetailContentBean;
import com.sgcc.sx.pmos.common.widget.CustomizeHorizontalScrollView;
import com.sgcc.pmos.nativesdk.R;

import java.util.ArrayList;
import java.util.List;

public class ComparisonDetailContentAdapter extends BaseMultiItemQuickAdapter<ComparisonDetailContentBean, BaseViewHolder> {

    /**
     * 保存列表ViewHolder集合
     */
    private List<BaseViewHolder> baseViewHolders = new ArrayList<>();

    /**
     * 记录item滑动的位置，用于RecyclerView上下滚动时更新所有列表
     */
    private int offestX;

    private OnTabScrollViewListener onTabScrollViewListener;

    private int width;
    public ComparisonDetailContentAdapter() {
        super();
        addItemType(ComparisonDetailContentBean.TITLE, R.layout.item_comparison_detail_title);
        addItemType(ComparisonDetailContentBean.CONTENT, R.layout.item_comparison_detail_content);
        addChildClickViewIds(R.id.tvComparisonRightTitle);
    }

    public void  setItemWidth(int width){
        this.width = width;
    }

    @Override
    protected void convert(BaseViewHolder helper, ComparisonDetailContentBean comparisonDetailContentBean) {
        switch (getDefItemViewType(getItemPosition(comparisonDetailContentBean))) {
            case ComparisonDetailContentBean.TITLE:
                helper.setText(R.id.tvComparisonTitle, comparisonDetailContentBean.getTitleName())
                        .setText(R.id.tvComparisonRightTitle, comparisonDetailContentBean.getRightText())
                        .setImageResource(R.id.imgIcon, comparisonDetailContentBean.getIconRes());
                break;
            case ComparisonDetailContentBean.CONTENT:
                RecyclerView contentRecyclerView = helper.getView(R.id.contentList);
                TextView contentName = helper.getView(R.id.contentName);
                contentName.setText(comparisonDetailContentBean.getContentTitleName());

                contentRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
                contentRecyclerView.setNestedScrollingEnabled(false);

                ContentAdapter contentAdapter = new ContentAdapter(comparisonDetailContentBean.getContentList(),width);
                contentRecyclerView.setAdapter(contentAdapter);
                CustomizeHorizontalScrollView contentScrollView = helper.getView(R.id.contentScrollView);
                if (!baseViewHolders.contains(helper)) {
                    baseViewHolders.add(helper);
                }
                contentAdapter.setOnItemClickListener((adapter, view, position) -> {

                    String text = comparisonDetailContentBean.getContentList().get(position).getText();

                });

                contentAdapter.setOnTextMeasuredHeightListener(maxHeight -> {
                    if (maxHeight > 0) {
                        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) contentName.getLayoutParams();
                        layoutParams.height = maxHeight;
                        contentName.setLayoutParams(layoutParams);
                    }
                });


                /**
                 * 水平滑动item时，BaseViewHolder，使得整个列表的HorizontalScrollView同步滚动
                 */
                contentScrollView.setViewListener((l, t, oldl, oldt) -> {
                    for (BaseViewHolder viewHolder : baseViewHolders) {
                        if (viewHolder != helper) {
                            viewHolder.getView(R.id.contentScrollView).scrollTo(l, 0);
                        }
                    }
                    /**
                     * 水平滑动item时，接口回调到Tab栏的HorizontalScrollView，使得Tab栏跟随item滚动实时更新
                     */
                    if (onTabScrollViewListener != null) {
                        onTabScrollViewListener.scrollTo(l, t);
                        offestX = l;
                    }
                });


                break;
            default:
                break;
        }


    }

    public List<BaseViewHolder> getBaseViewHolders() {
        return baseViewHolders;
    }

    public int getOffestX() {
        return offestX;
    }

    public void setOnTabScrollViewListener(OnTabScrollViewListener onTabScrollViewListener) {
        this.onTabScrollViewListener = onTabScrollViewListener;
    }

    public interface OnTabScrollViewListener {
        void scrollTo(int l, int t);
    }
}
